Monday, June 25, 2012

Javascript filter Table/GridView Search Pop Row example

well, in my workplace the guys used the jq grid plugin and changed acostumized it, but it seems too much work for doing just the search pop row, so that how it went:

*notice that in my version there are columns to be hidden, so if u dont have some like that u can just skip all the "display == "none")" parts.

part 1: create the pop row with txt's,
<script type="text/javascript">
$(function () {
    var table = document.getElementById("dgvUsersCompanies");
    var body = table.getElementsByTagName('tbody')[0];
    var headerRow = body.rows[0];

    var row = body.insertRow(1);
    row.setAttribute("id", "theSearchRow");

    var cell0 = row.insertCell(0);
    cell0.innerHTML =
       "<input type='button' value='Search:' onclick='FilterDGV()' />";
    for (var i = 1; i < headerRow.cells.length; i++) {
       if (headerRow.cells[i].style.display == "none") {
           var cell = row.insertCell(i);
           cell.setAttribute("class", "cellDisplayNone");
       }
      
else {
          var headWidth = $(headerRow.cells[i]).width();
          var cell = row.insertCell(i);
          cell.innerHTML = "<input id='" + headerRow.cells[i].innetText +
              "Search' type='text'" + " style='width:" +
              headWidth + "px' value=''" +
              " onkeypress='{if (event.keyCode==13) FilterDGV();}' />";
       }
    }
               

    document.getElementById("theSearchRow").style.display = "none";
    $("#btnMakeSearch").click(function () {
         var thediv = document.getElementById("theSearchRow");
         if (thediv.style.display != "none") {
             thediv.style.display = "none"
         }
         else {
            thediv.style.display = "block"
         }
    });
});
</script>

part 2: FITLER!
<script type="text/javascript">
function FilterDGV() {
     var table = document.getElementById("dgvUsersCompanies");
     var body = table.getElementsByTagName('tbody')[0];
     var rows = body.rows;
     var searchRowCells = document.getElementById("theSearchRow").cells;

     for (var i = 2; i < rows.length; i++) {
         rows[i].style.display = "block";

         for (var j = 1; j < rows[i].cells.length; j++) {
             if (rows[0].cells[j].style.display == "none") {
                 continue;
             }

             var s = rows[i].cells[j].innerText.toLowerCase();
             var s2 = searchRowCells[j].firstChild.value.toLowerCase();
             if (s2 != "" && s.indexOf(s2) == -1) {
                rows[i].style.display =
"none";
             }
         }
    }
}
</script>


my 1st real js duty, hope u like it
example with the button (my original table had checkbox in col 0)
http://jsbin.com/ixadic/12/edit#preview
and here is a version W/O the search btn in col 0 but another search text instead
http://jsbin.com/ixadic/13/edit#preview
THX to RNAN for the free table

Tuesday, June 5, 2012

c# Service Play Sound with NAudio example by Moshe

windows server 2008 or vista forword if u host a service W/O a user loggon the system wont start the audio devices.
to solve this u can use the dear NAudio Dll from http://naudio.codeplex.com/ - its open source.
but its a damn long (and helpful) code while u wont a simple service with only 1 CS page, so my Dear Team Chief and IT man Moshe reduced that to a single CS page just to play the sounds and here u go:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;
using NAudio.CoreAudioApi;
using System.ComponentModel.Composition;
using NAudio.Wave.SampleProviders;

namespace CallAndRead
{
   
public interface IInputFileFormatPlugin
   {
      
string Name { get; }
       string Extension { get; }
       WaveStream CreateWaveStream(string fileName);
   }
   

   [Export(typeof(IInputFileFormatPlugin))]
   class WaveInputFilePlugin : IInputFileFormatPlugin
   {
       public string Name
      { get { return "WAV file"; } }
       public string Extension
      { get { return ".wav"; } }

       public WaveStream CreateWaveStream(string fileName)
       {
             WaveStream readerStream = new WaveFileReader(fileName);
             if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm
                   && readerStream.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat)
            {
                  readerStream =
WaveFormatConversionStream.CreatePcmStream(readerStream);
                  readerStream = new BlockAlignReductionStream(readerStream);
            }
            return readerStream;
      }
   }
   

   class WASAPI
   {
        public static void Concatenate(string outputFile, IEnumerable<string> sourceFiles)
        {
             byte[] buffer = new byte[1024];
             WaveFileWriter waveFileWriter = null;
             try
            {
                 foreach (string sourceFile in sourceFiles)
                 {
                        using (WaveFileReader reader = new WaveFileReader(sourceFile))
                        {
                               if (waveFileWriter == null)
                                   waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                               else
                              {
                                     if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                                     throw new InvalidOperationException(
                                              "Can't concatenate WAV Files that don't share the same format");
                              }
                              int read;
                              while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                             {
                                   waveFileWriter.WriteData(buffer, 0, read);
                             }
                      }
                }
            }
           
finally
           {
                if (waveFileWriter != null)
                     waveFileWriter.Dispose();
           }
      }

      private IWavePlayer waveOut = new WasapiOut(NAudio.CoreAudioApi.AudioClientShareMode.Shared, 300);
      WaveStream fileWaveStream;
      Action<float> setVolumeDelegate;

     [ImportMany(typeof(IInputFileFormatPlugin))]
     public IEnumerable<IInputFileFormatPlugin> InputFileFormats { get; set; }

     void OnPreVolumeMeter(object sender, NAudio.Wave.SampleProviders.StreamVolumeEventArgs e)
     {
          // we know it is stereo
          //w aveformPainter1.AddMax(e.MaxSampleValues[0]);
          //waveformPainter2.AddMax(e.MaxSampleValues[1]);
     }

      public ISampleProvider CreateInputStream(string fileName)
     {
  
        var plugin = new WaveInputFilePlugin();
           if (plugin == null)
                  throw new InvalidOperationException("Unsupported file extension");
           fileWaveStream = plugin.CreateWaveStream(fileName);
           var waveChannel = new NAudio.Wave.SampleProviders.SampleChannel(fileWaveStream);
           setVolumeDelegate = (vol) => waveChannel.Volume = vol;
           waveChannel.PreVolumeMeter += OnPreVolumeMeter;
           var postVolumeMeter = new MeteringSampleProvider(waveChannel);
           postVolumeMeter.StreamVolume += OnPostVolumeMeter;
           return postVolumeMeter;
     }

     
void OnPostVolumeMeter(object sender, StreamVolumeEventArgs e)
     {
           // we know it is stereo
           //volumeMeter1.Amplitude = e.MaxSampleValues[0];
          //volumeMeter2.Amplitude = e.MaxSampleValues[1];
      }

      public WASAPI(string fileName)
     {
            ISampleProvider sampleProvider = null;
            sampleProvider = CreateInputStream(fileName);
            waveOut.Init(
new SampleToWaveProvider(sampleProvider));
            waveOut.Play();
            return;
      }
   }
}


and what u need to do in ur app is simply this:
WASAPI Player = new WASAPI(path);
(advised to do sleep for it to finish and maybe player = null if ur the saving resources type)

ENJOY!